//
//  CZStatusToolBar.swift
//  WeiBoNew
//
//  Created by 秦川 on 16/5/14.
//  Copyright © 2016年 秦川. All rights reserved.
//

import UIKit

class CZStatusToolBar: UIView {
    
    
    //三个button
    private lazy var retweetButton: UIButton = self.addChildButton("timeline_icon_retweet", title: "转发")
    private lazy var commentButton: UIButton = self.addChildButton("timeline_icon_comment", title: "评论")
    private lazy var unlikeButton: UIButton = self.addChildButton("timeline_icon_unlike", title: "赞")
    var statusViewModel: CZStatusViewModel?{
        didSet{
//        let retweetCount = statusViewModel?.status?.reposts_count ?? 0
//         let commentCount = statusViewModel?.status?.comments_count ?? 0
//         let unlikeCount = statusViewModel?.status?.attitudes_count ?? 0
        retweetButton.setTitle(statusViewModel?.retweetCountContent, forState: .Normal)
         commentButton.setTitle(statusViewModel?.commentCountContent, forState: .Normal)
             unlikeButton.setTitle(statusViewModel?.unlikeCountContent, forState: .Normal)
        }
    
    }
    
    //手写代码创建
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
//        backgroundColor = RandomColor()
        let firstLine = addChildLineView()
        let secondLine = addChildLineView()
        
        
       retweetButton.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(self)
        make.leading.equalTo(self)
        make.bottom.equalTo(self)
        make.width.equalTo(commentButton)
        
        }
        commentButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(retweetButton.snp_trailing)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton)
        }
        unlikeButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(commentButton.snp_trailing)
            make.bottom.equalTo(self)
            make.trailing.equalTo(self)
        }
        firstLine.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton.snp_trailing)
            make.centerY.equalTo(self)
        }
        secondLine.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentButton.snp_trailing)
        }
        
    }
    //增加button的方法
    private func addChildButton(imageName: String,title: String) ->UIButton{
        let button = UIButton()
        button.setImage(UIImage(named: imageName) , forState: .Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitle(title, forState: .Normal)
        //去掉点击效果
//        方法一 //  1.把背景图片作为按钮的背景
        //button.backgroundColor = UIColor(patternImage: UIImage(named: "timeline_card_bottom_background")!)
        ////去掉点击效果
       button.adjustsImageWhenHighlighted = false
        
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        addSubview(button)
        return button
    }
    //增加线的方法
    private func addChildLineView() ->UIView{
        
        let view = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
//    let view = UIView(frame: CGRect(x: 0, y: 0, width: 2, height: 30))
//        view.backgroundColor = UIColor.redColor()
        
        addSubview(view)
        return view
    }
}
